from google.appengine.ext import db

from gaeo.controller import BaseController

from model.person import Person

class PersonController(BaseController):
    def create(self):
        r = Person(
            no = self.params.get('no', None),
            name = self.params.get('name', None),
            nation = self.params.get('nation', None),
            sex = self.params.get('sex', None),
            sport = self.params.get('sport', None),
        )
        r.put()
        self.redirect('/person')

    def destroy(self):
        r = Person.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/person')

    def edit(self):
        rec = Person.get(self.params.get('key'))
        if rec:
            setattr(self, 'no', rec.no)
            setattr(self, 'name', rec.name)
            setattr(self, 'nation', rec.nation)
            setattr(self, 'sex', rec.sex)
            setattr(self, 'sport', rec.sport)
        else:
            setattr(self, 'no', 'Error')
            setattr(self, 'name', 'Error')
            setattr(self, 'nation', 'Error')
            setattr(self, 'sex', 'Error')
            setattr(self, 'sport', 'Error')
                    
        self.flagSelected()

    def index(self):
        query = Person.all()
        self.result = query
        self.flagSelected()

    def new(self):
        self.flagSelected()

    def show(self):
        rec = Person.get(self.params.get('key'))
        if rec:
            setattr(self, 'no', rec.no)
            setattr(self, 'name', rec.name)
            setattr(self, 'nation', rec.nation)
            setattr(self, 'sex', rec.sex)
            setattr(self, 'sport', rec.sport)
        else:
            setattr(self, 'no', 'Error')
            setattr(self, 'name', 'Error')
            setattr(self, 'nation', 'Error')
            setattr(self, 'sex', 'Error')
            setattr(self, 'sport', 'Error')
            
            self.flagSelected()

    def update(self):
        r = Person.get(self.params.get('key'))
        r.no = self.params.get('no', None)
        r.name = self.params.get('name', None)
        r.nation = self.params.get('nation', None)
        r.sex = self.params.get('sex', None)
        r.sport = self.params.get('sport', None)
        r.put()
        self.redirect('/person')
        
     # ----- Not Action --------------------------   
    def flagSelected(self):
        self.selected = 'person'
